# -*- coding: utf-8 -*-
import os, csv, getopt, sys, socket


if __name__ == '__main__':
    opts, args = getopt.getopt(sys.argv[1:], '-h-p', ['host=', 'port='])
    d = dict(opts)
    if(len(opts) < 2):
        print("Parameter Error")
        exit()
    host = d.get("--host")
    port = d.get("--port")
    if(len(host.split(".")) < 4):
        print("Parameter Error")
        exit()
    # 创建 socket 对象
    s = socket.socket()
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # 定义socket类型，网络通信，TCP
    s.settimeout(0.1)# 设置超时，防止脚本卡住
    p = port.split("-")
    min = int(p[0])
    max = min + 1
    if(len(p) > 1):
        max = int(p[1]) + 1
    for i in range(min,max):
        try:
            s.connect((host, i))  # 要连接的IP与端口
        except Exception as e:
            print(str(i) + "  Close")
            continue
        print(str(i) + "  Open")